package com.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.mapper.LoginDao;
import com.dao.mapper.PomrDao;
import com.dao.mapper.UserDao;
import com.domain.UserLogin;
import com.service.UserLoginService;
import com.utils.ObjectUtils;
import com.utils.Ret;

@Service
public class UserLoginServiceImlp implements UserLoginService {

	@Autowired
	LoginDao loginDao;
	@Autowired
	PomrDao pomrDao;
	
	
	
	@Override
	public Ret doLogin(String code, String ucode, String pwd,
			HttpServletRequest request) {
		Ret ret = new Ret();

		// 验证码校验
		if ("".equals(code)) {
			ret.setStatusCode("101");
			ret.setMessage("验证码不能为空！");
		}if (ret.getMessage().length() == 0) {
			String _code = (String) request.getSession().getAttribute("captcha");
			
//			if (!"-1".equals(code) && !code.toLowerCase().equals(_code.toLowerCase())) {
			if(false){
				ret.setStatusCode("103");
				ret.setMessage("验证码输入错误，请重新输入！");
			}else{
				UserLogin userLogin = loginDao.getUserLogin2(ucode, pwd);
				if (null == userLogin) {
					ret.setMessage("输入用户信息有误！");
					ret.setStatusCode("301");
				} else {
					ret.setStatusCode("200");
					request.getSession().invalidate();
					HttpSession newsession = request.getSession(true);
					newsession.setAttribute("usertype", userLogin.getType());
					newsession.setAttribute("usercode", userLogin.getUcode());
					newsession.setAttribute("username", userLogin.getUname());
					newsession.setAttribute("ocode", userLogin.getOcode());
					
				}
			}
		}

		return ret;
	}

	@Override
	public List<Map<String, Object>> getAuth(int type) {
		
		return loginDao.getAuth(type);
	}

	@Override
	public Map<String,Object> doRegist(String zjh, String xm, String pwd, String pwd2) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		int ucode = 0;
		try {
			Map<String,Object> hasZjh = pomrDao.getUidByZjh(zjh);
			if(hasZjh == null){//有没有这个人
				result.put("rcode", "300");
				result.put("message", "健康档案不存在该用户！");
			}else{
				ucode = ObjectUtils.getIntVal(hasZjh.get("ucode").toString());
				int exist = loginDao.getCountOfUcode(ucode);
				if(exist==1){
					result.put("rcode", "300");
					result.put("message", "该用户已经注册，不能重复注册！");
				}else{//开着注册
					
					if(pwd.equals(pwd2)){
						UserLogin userLogin = new UserLogin();
						userLogin.setOcode(ObjectUtils.getStrVal(hasZjh.get("ocode")));
						userLogin.setType(3);
						userLogin.setUcode(ObjectUtils.getStrVal(hasZjh.get("ucode")));
						userLogin.setUname(xm);
						userLogin.setPwd(pwd);
						loginDao.addUserLogin(userLogin);
						result.put("rcode", "200");
						result.put("message", "注册成功，用户代码为："+ucode);
					}else{
						result.put("rcode", "300");
						result.put("message", "密码不一致！");
					}
					
				}
			}
			
	} catch(Exception e){
		e.printStackTrace();result.put("rcode", "300");
		result.put("message", "注册失败！");
	}
		

		return result;
	}
}
